package com.purong.purong.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.purong.purong.R;
import com.purong.purong.bean.BankLoanMaterItem;
import com.purong.purong.util.CommonUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zengwenjie on 2017/11/23.
 */
public class BankLoanApplyMaterAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<BankLoanMaterItem> materList=new ArrayList<>();
    private Context mContext;
    private OnMaterCliclListener mListener;
    public BankLoanApplyMaterAdapter(Context context){
        this.mContext=context;
    }
    public void setData(List<BankLoanMaterItem> List) {

        this.materList = List;
    }
    public void setOnMaterCliclListener(OnMaterCliclListener listener){
        this.mListener=listener;
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v;
        if (viewType== BankLoanMaterItem.TYPE_ONE){
            v = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.loan_material_item_one, parent, false);
            // set the view's size, margins, paddings and item_circle parameters
            return  new ViewHolderOne(v);
        }if (viewType== BankLoanMaterItem.TYPE_TWO){
            v = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.loan_material_item_two, parent, false);
            // set the view's size, margins, paddings and item_circle parameters
            return  new ViewHolderTwo(v);
        }if (viewType== BankLoanMaterItem.TYPE_THREE){
            v = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.loan_material_item_three, parent, false);
            // set the view's size, margins, paddings and item_circle parameters
            return  new ViewHolderThree(v);
        }else {
            v = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.loan_material_item_four, parent, false);
            // set the view's size, margins, paddings and item_circle parameters
            return  new ViewHolderFour(v);
        }
    }
    public class ViewHolderOne extends MaterHolder{
        //绑定每个CircleItem的控件引用到ViewHolder
        private TextView title,content;
        private LinearLayout mainLay;
        public ViewHolderOne(View v) {
            super(v);
            title=(TextView)v.findViewById(R.id.title);
            content=(TextView)v.findViewById(R.id.content);
            mainLay=(LinearLayout) v.findViewById(R.id.main_lay);
        }
    }
    public class ViewHolderTwo extends MaterHolder {
        //绑定每个CircleItem的控件引用到ViewHolder
        private TextView title,content;
        private LinearLayout mainLay;
        public ViewHolderTwo(View v) {
            super(v);
            title=(TextView)v.findViewById(R.id.title);
            content=(TextView)v.findViewById(R.id.content);
            mainLay=(LinearLayout) v.findViewById(R.id.main_lay);
        }
    }
    public class ViewHolderThree extends MaterHolder {
        //绑定每个CircleItem的控件引用到ViewHolder
        private TextView title,unit;
        private EditText editText;
        private LinearLayout mainLay;
        public ViewHolderThree(View v) {
            super(v);
            title=(TextView)v.findViewById(R.id.title);
            editText=(EditText)v.findViewById(R.id.edit_text);
            unit=(TextView)v.findViewById(R.id.unit);
            mainLay=(LinearLayout) v.findViewById(R.id.main_lay);
        }
    }
    public class ViewHolderFour extends MaterHolder {
        //绑定每个CircleItem的控件引用到ViewHolder
        private TextView title,unit;
        private EditText editText;
        private LinearLayout mainLay;
        public ViewHolderFour(View v) {
            super(v);
            title=(TextView)v.findViewById(R.id.title);
            editText=(EditText)v.findViewById(R.id.edit_text);
            unit=(TextView)v.findViewById(R.id.unit);
            mainLay=(LinearLayout) v.findViewById(R.id.main_lay);
        }
    }
    public class MaterHolder extends RecyclerView.ViewHolder{
        public MaterHolder(View itemView) {
            super(itemView);
        }

        public void setVisibility(boolean isVisible){
            RecyclerView.LayoutParams param = (RecyclerView.LayoutParams)itemView.getLayoutParams();
            if (isVisible){
                param.height = CommonUtil.dip2px(mContext,44);
                param.width = LinearLayout.LayoutParams.MATCH_PARENT;
                itemView.setVisibility(View.VISIBLE);
            }else{
                itemView.setVisibility(View.GONE);
                param.height = 0;
                param.width = 0;
            }
            itemView.setLayoutParams(param);
        }
    }
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (getItemViewType(position)== BankLoanMaterItem.TYPE_ONE){
            ((ViewHolderOne)holder).title.setText(materList.get(position).getMaterial_item_name());
            if (materList.get(position).getChose_chlid_id().equals("-1")){
                ((ViewHolderOne)holder).content.setText("请选择");
                ((ViewHolderOne)holder).content.setTextColor(mContext.getResources().getColor(R.color.text_gray));
            }else {
                ((ViewHolderOne)holder).content.setText(materList.get(position).getChose_chlid_name());
                ((ViewHolderOne)holder).content.setTextColor(mContext.getResources().getColor(R.color.blue));
            }
            ((ViewHolderOne)holder).mainLay.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mListener!=null){
                        mListener.onItemClick(position, BankLoanMaterItem.TYPE_ONE,materList.get(position));
                    }
                }
            });

        }else if (getItemViewType(position)== BankLoanMaterItem.TYPE_TWO){
            ((ViewHolderTwo)holder).title.setText(materList.get(position).getMaterial_item_name());
            if (materList.get(position).getChose_chlid_id().equals("-1")){
                ((ViewHolderTwo)holder).content.setText("请选择");
                ((ViewHolderTwo)holder).content.setTextColor(mContext.getResources().getColor(R.color.text_gray));
            }else {
                ((ViewHolderTwo)holder).content.setText(materList.get(position).getChose_chlid_name());
                ((ViewHolderTwo)holder).content.setTextColor(mContext.getResources().getColor(R.color.blue));
            }
            ((ViewHolderTwo)holder).mainLay.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mListener!=null){
                        mListener.onItemClick(position, BankLoanMaterItem.TYPE_ONE,materList.get(position));
                    }
                }
            });

        }else if (getItemViewType(position)== BankLoanMaterItem.TYPE_THREE){
            ((ViewHolderThree)holder).title.setText(materList.get(position).getMaterial_item_name());
            ((ViewHolderThree)holder).editText.setText(materList.get(position).getInput_content());
            ((ViewHolderThree)holder).unit.setText(materList.get(position).getMaterial_item_unit());
            ((ViewHolderThree)holder).mainLay.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mListener!=null){
                        mListener.onItemClick(position, BankLoanMaterItem.TYPE_ONE,materList.get(position));
                    }
                }
            });
            ((ViewHolderThree)holder).editText.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    materList.get(position).setInput_content(s.toString());
                }

                @Override
                public void afterTextChanged(Editable s) {

                }
            });
            setEditText(((ViewHolderThree)holder).editText,materList.get(position).getMaterial_item_type());
        }else if (getItemViewType(position)== BankLoanMaterItem.TYPE_FOUR){
            ((ViewHolderFour)holder).title.setText(materList.get(position).getMaterial_item_name());
            ((ViewHolderFour)holder).editText.setText(materList.get(position).getInput_content());
            ((ViewHolderFour)holder).unit.setText(materList.get(position).getMaterial_item_unit());
            ((ViewHolderFour)holder).mainLay.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mListener!=null){
                        mListener.onItemClick(position, BankLoanMaterItem.TYPE_ONE,materList.get(position));
                    }
                }
            });
            ((ViewHolderFour)holder).editText.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    materList.get(position).setInput_content(s.toString());
                }

                @Override
                public void afterTextChanged(Editable s) {

                }
            });
            setEditText(((ViewHolderFour)holder).editText,materList.get(position).getMaterial_item_type());
        }
    }
    private void setEditText(EditText et,String type){

        switch (type){
            case "2":
                et.setInputType(InputType.TYPE_CLASS_NUMBER|InputType.TYPE_NUMBER_VARIATION_NORMAL);
                break;
            case "3":
                et.setInputType(InputType.TYPE_CLASS_NUMBER|InputType.TYPE_NUMBER_FLAG_DECIMAL);
                et.addTextChangedListener(new DoubleTextWatch(et));
                break;
            case "1":
            case "4":
                et.setInputType(InputType.TYPE_CLASS_TEXT |InputType.TYPE_TEXT_VARIATION_NORMAL);
                break;
        }
    }
    @Override
    public int getItemViewType(int position) {
        return materList.get(position).getType();
    }
    @Override
    public int getItemCount() {
        return materList.size();
    }
    public interface OnMaterCliclListener{
        void onItemClick(int position,int type,BankLoanMaterItem item);
    }

    private class DoubleTextWatch implements TextWatcher{
        private EditText editText;
        public DoubleTextWatch(EditText ed){
            editText=ed;
        }
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

            if (s.toString().contains(".")) {
                if (s.toString().startsWith(".")) {
                    editText.setText("0"+s);
                }else if (s.length() - 1 - s.toString().indexOf(".") > 2) {
                    s = s.toString().subSequence(0,
                            s.toString().indexOf(".") + 3);
                    editText.setText(s);
                    editText.setSelection(s.length());
                }
            }

            if (s.toString().startsWith("0")
                    && s.toString().trim().length() > 1) {
                if (!s.toString().substring(1, 2).equals(".")) {
                    editText.setText(s.toString().replaceFirst("0",""));
                }
            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    }
}
